using FileTransferDesktop.Models;

namespace FileTransferDesktop.Services;

/// <summary>
/// 应用设置服务接口
/// </summary>
public interface IAppSettingsService
{
    /// <summary>
    /// 获取应用设置
    /// </summary>
    AppSettings GetSettings();

    /// <summary>
    /// 异步获取应用设置
    /// </summary>
    Task<AppSettings> GetSettingsAsync();

    /// <summary>
    /// 保存应用设置
    /// </summary>
    void SaveSettings(AppSettings settings);

    /// <summary>
    /// 异步保存应用设置
    /// </summary>
    Task SaveSettingsAsync(AppSettings settings);

    /// <summary>
    /// 获取下载路径
    /// </summary>
    string GetDownloadPath();

    /// <summary>
    /// 设置下载路径
    /// </summary>
    void SetDownloadPath(string path);

    /// <summary>
    /// 重置为默认设置
    /// </summary>
    void ResetToDefaults();

    /// <summary>
    /// 设置发生变化事件
    /// </summary>
    event EventHandler<AppSettings>? SettingsChanged;
}

/// <summary>
/// 应用设置服务实现
/// </summary>
public class AppSettingsService : IAppSettingsService
{
    private AppSettings? _cachedSettings;
    private readonly object _lock = new();

    public event EventHandler<AppSettings>? SettingsChanged;

    /// <summary>
    /// 获取应用设置
    /// </summary>
    public AppSettings GetSettings()
    {
        lock (_lock)
        {
            if (_cachedSettings != null)
                return _cachedSettings;

            // 从数据库加载设置
            _cachedSettings = LocalDbService.ExecuteDbOperation(db =>
            {
                var col = db.GetCollection<AppSettings>("app_settings");
                return col.FindById("app_settings") ?? new AppSettings();
            });

            return _cachedSettings;
        }
    }

    /// <summary>
    /// 异步获取应用设置
    /// </summary>
    public async Task<AppSettings> GetSettingsAsync()
    {
        return await Task.Run(GetSettings);
    }

    /// <summary>
    /// 保存应用设置
    /// </summary>
    public void SaveSettings(AppSettings settings)
    {
        lock (_lock)
        {
            settings.UpdatedAt = DateTime.Now;

            LocalDbService.ExecuteDbOperation(db =>
            {
                var col = db.GetCollection<AppSettings>("app_settings");
                col.Upsert(settings);
                return true;
            });

            _cachedSettings = settings;

            // 触发设置变化事件
            SettingsChanged?.Invoke(this, settings);
        }
    }

    /// <summary>
    /// 异步保存应用设置
    /// </summary>
    public async Task SaveSettingsAsync(AppSettings settings)
    {
        await Task.Run(() => SaveSettings(settings));
    }

    /// <summary>
    /// 获取下载路径
    /// </summary>
    public string GetDownloadPath()
    {
        return GetSettings().DownloadPath;
    }

    /// <summary>
    /// 设置下载路径
    /// </summary>
    public void SetDownloadPath(string path)
    {
        var settings = GetSettings();
        settings.DownloadPath = path;
        SaveSettings(settings);
    }

    /// <summary>
    /// 重置为默认设置
    /// </summary>
    public void ResetToDefaults()
    {
        var defaultSettings = new AppSettings();
        SaveSettings(defaultSettings);
    }
}
